package org.xjtu.weteam.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xjtu.weteam.common.utils.imageUtil;
import org.xjtu.weteam.mapper.UserTaskMapper;
import org.xjtu.weteam.model.domain.User;
import org.xjtu.weteam.model.domain.UserTask;
import org.xjtu.weteam.service.UserTaskService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kevin
 */
@Service
public class UserTaskServiceImpl extends ServiceImpl<UserTaskMapper, UserTask> implements UserTaskService {
    @Autowired
    private UserTaskMapper userTaskMapper;

    @Override
    public Boolean saveBatchs(ArrayList<UserTask> userTasks) {
        for(UserTask userTask: userTasks){
            userTaskMapper.insertRelationship(userTask);
        }
        return true;
    }

    @Override
    public List<String> searchUserProfilesByTaskId(Integer taskId) throws IOException {
        List<String> userProfiles = userTaskMapper.searchUserProfilesByTaskId(taskId);
        List<String> userProfilesBase64 = new ArrayList<>();
        for (String userProfile : userProfiles)
            userProfilesBase64.add(imageUtil.getAvatar(userProfile));
        return userProfilesBase64;
    }

    @Override
    public List<User> getTaskUsers(Integer taskId) {
        return userTaskMapper.getTaskUsers(taskId);
    }

    @Override
    public void removeRelationship(Integer taskId, List<Integer> userIds) {
        for(Integer userId:userIds){
            userTaskMapper.deleteRelationship(taskId, userId);
        }
    }
}
